package com.xb.p4_widgets;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class CheckBoxActivity extends AppCompatActivity {
    TextView tv_hobby;
    Button btn_sport;

    CheckBox cb1;
    CheckBox cb2;
    CheckBox cb3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);

        tv_hobby=findViewById(R.id.tv_hobby);
        btn_sport=findViewById(R.id.btn_sport);

        cb1=findViewById(R.id.sport1);
        cb2=findViewById(R.id.sport2);
        cb3=findViewById(R.id.sport3);

        btn_sport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str="";

                if(cb1.isChecked()){
                    str+="\n"+cb1.getText();
                }
                if(cb2.isChecked()){
                    str+="\n"+cb2.getText();
                }
                if(cb3.isChecked()){
                    str+="\n"+cb3.getText();
                }
                tv_hobby.setText("您选择了："+str);
            }
        });
    }

    public void onCheckBoxClicked(View view){
        //使用Toast
        if( ((CheckBox)view).isChecked()){//选中
            Toast.makeText(CheckBoxActivity.this, ((CheckBox)view).getText(),Toast.LENGTH_SHORT).show();
        }
    }


}
